package com.bicon.botu.mall.component.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.common.SearchConstants;
import com.bicon.botu.mall.component.entity.HistorySearch;
import com.bicon.botu.mall.component.entity.HotSearch;
import com.bicon.botu.mall.component.service.IHotSearchService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 热搜 前端控制器
 *
 * @author 张凡
 * @date 2017/8/7 17:07.
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/hotSearch")
public class HotSearchController extends BaseController {
    @Autowired
    IHotSearchService iHotSearchService;

    /**
     * 新增热词
     *
     * @param type     模块类型：商城-mall,医疗-medical（其他待定）
     * @param hotWord  热词
     * @param showFlag 显示开关：0显示，1不显示
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam("type") String type,
                             @RequestParam("hotWord") String hotWord,
                             @RequestParam("showFlag") String showFlag) {
        //限制新增热词长度不能超过4个字符
        if (hotWord.length() > 4) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //查询当前热词个数
        Integer num = new HotSearch().selectCount(new EntityWrapper<HotSearch>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .eq("type", type));
        if (num >= 5) {
            return new BaseResult(SearchConstants.RESPONSE_CODE_604, SearchConstants.RESPONSE_MESSAGE_604);
        }
        //查询数据库是否已存在此热词
        HotSearch hotSearch1 = new HotSearch().selectOne(new EntityWrapper<HistorySearch>()
                .eq("hot_word", hotWord).eq("type", type).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (hotSearch1 != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        return iHotSearchService.insert(type, hotWord, showFlag,num);
    }

    /**
     * 删除热词
     *
     * @param type   模块类型：商城-mall,医疗-medical（其他待定）
     * @param idJson 热词Id集合
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam("type") String type,
                             @RequestParam("idJson") String idJson) {
        return iHotSearchService.delete(type, idJson);
    }

    /**
     * 修改热词
     *
     * @param id       热词id
     * @param hotWord  热词
     * @param showFlag 显示开关：0显示，1不显示
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("id") String id,
                             @RequestParam(value = "hotWord", required = false) String hotWord,
                             @RequestParam(value = "showFlag", required = false) String showFlag) {
        //判断id是否存在
        HotSearch hotSearch = new HotSearch().selectById(id);
        if (hotSearch == null) {
            return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
        //查询数据库是否已存在此热词
        HotSearch hotSearch1 = new HotSearch().selectOne(new EntityWrapper<HistorySearch>()
                .eq("hot_word", hotWord).eq("type", hotSearch.getType()).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (hotSearch1 != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        return iHotSearchService.update(id, hotWord, showFlag);
    }


    /**
     * 初始化所有热词到redis
     *
     * @param type 模块类型：商城-mall,医疗-medical（其他待定）
     * @return
     */
    @RequestMapping(value = "/initHotSearchToRedis", method = RequestMethod.POST)
    public BaseResult initHotSearchToRedis(@RequestParam("type") String type) {
        return iHotSearchService.initHotSearchToRedis(type);
    }

    /**
     * 获取全部热搜
     *
     * @param type 模块类型：全局-all 商城-mall,医疗-medical（其他待定）
     * @return
     */
    @RequestMapping(value = "/getHotSearch", method = RequestMethod.POST)
    public BaseResult getHotSearchDetailFromRedis(@RequestParam("type") String type) {
        List<HotSearch> hotSearchList = new HotSearch().selectList(new EntityWrapper<HotSearch>()
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .eq("type", type).orderBy("sort", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, hotSearchList);
    }

}
